// pages/listen/listen.js
let num;
let number=0;
let number2=0;
// let flag=true;
let flag2=true;
let arr=new Array(),arrA=new Array(),collect=new Array()
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: { 
    singer:'',
    clickCollect:'',
    time:'',
    finger:'',
    src:'',
    img:'https://s3.bmp.ovh/imgs/2021/11/a67c923cd8edbf2a.png',
    id:'',
    collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c30.png',
    flag:false
  },


  onShow: function () {
    //拿到了
    arr=wx.getStorageSync('arr')
    collect=wx.getStorageSync('collect')
    arrA=wx.getStorageSync('arrA')
    var that=this

    that.setData({
          singer:arr.singer,
          time:arr.name,
          src:arr.src,
          id:arr.id
    })
    //判断collect里面是不是有当前这个arr的id
    for(let i=0;i<collect.length;i++){
      if(collect[i].music_id==arr.id){
        // console.log(collect[i])
        that.setData({
          clickCollect:'取消收藏',
          collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c18.png'
        })
      }
    }

    // this.innerAudioContextwx.onError((result) => {console.error(result)})

    // this.innerAudioContext.onError = err => {
    //   console.error('出错了', err)
    // }
    // 返回歌曲视频静态资源代码：c.setHeader('cache-control', 'max-age=600')
    // num=parseInt(Math.random()*arrA.length)
    let style=wx.getStorageSync('style')
    if(style!=''){
      this.setData({
        contentStyle:wx.getStorageSync('style')
      })
    }else{
      this.setData({
        contentStyle:'https://pic.imgdb.cn/item/6199ad6c2ab3f51d91e20c91.jpg'
      })
    }
    let children=wx.getStorageSync('children')
      if(children!==''){
        this.setData({
            children:wx.getStorageSync('children')
        })
      }else{
        this.setData({
            children:'https://pic.imgdb.cn/item/6199ab9b2ab3f51d91e171d6.png'
        })
      }
  },

  click:function(){
    this.setData({
      img:'https://s3.bmp.ovh/imgs/2021/11/a67c923cd8edbf2a.png'
    })
    if(this.data.flag==true){
      this.innerAudioContext.pause()
      this.setData({
        flag:false
      })
    }
    // this.innerAudioContext.pause()
    collect=wx.getStorageSync('collect')
    console.log(collect)
    number=0
    var that=this
    num=parseInt(Math.random()*arrA.length)  

    that.setData({
      singer:arrA[num].music_singer,
      time:arrA[num].music_name,
      src:arrA[num].music_url,
      id:arrA[num].music_id
    })
    for(let i=0;i<collect.length;i++){
      if(that.data.id==collect[i].music_id){
        number=number+1
        console.log(collect[i])
        that.setData({
          clickCollect:'取消收藏',
          collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c18.png'
        })
      
      }
    }
    if(number==0){
      console.log(2)
      that.setData({
        clickCollect:'点击收藏',
        collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c30.png'
      })
    }   
  },
  onReady:function(){
    this.innerAudioContext = wx.createInnerAudioContext();
  },
  play:function(){
    this.innerAudioContext.src=this.data.src 
    this.innerAudioContext.volume=1
    // this.innerAudioContext.play()
    
    if(this.data.flag==false){
      console.log(this.data.flag)
      //flag是true就是播放
      this.setData({
        img:'https://s3.bmp.ovh/imgs/2021/11/982f4f3e0af32e4e.png',
        flag:true
      })
      // flag=false;
      this.innerAudioContext.play()
      
    }else{
      console.log(this.data.flag)
      this.setData({
        img:'https://s3.bmp.ovh/imgs/2021/11/a67c923cd8edbf2a.png',
        flag:false
      })
      // flag=true;
      this.innerAudioContext.pause()
    
    }
  },
  onHide:function(){
    if(this.data.flag==true){ 
      this.innerAudioContext.stop()
      this.innerAudioContext.destory()
    }
    
  },
  onUnload:function(){
    if(this.data.flag==true){
      this.innerAudioContext.stop()
      this.innerAudioContext.destory()
    }
    
  },
  collect:function(){
   collect=wx.getStorageSync('collect')
    //先判断
    //flag为true是收藏
    number2=0
    // console.log(arrA)
    var that=this
    //是红心
    for(let i=0;i<collect.length;i++){
      if(that.data.id==collect[i].music_id){
        number2++
    
        console.log(that.data.id)
        console.log(collect[i])

      }
    }
    console.log(number2)
    if(number2==0){
      wx.request({
        url: 'https://wx.xxmtcds.top:2333/user/collectmusic',
        method:'POST',
        name:'collectList',
        data:{
          id:that.data.id
        },
        header:{
          'Content-Type': 'application/json',
          authorization:app.globalData.token
        },
        success:function(res){
          console.log(res.data)
          wx.request({
            url: 'https://wx.xxmtcds.top:2333/user/collectmusic',
            method:'GET',
            name:'collectList',
            header:{
              'Content-Type': 'application/json',
                authorization:app.globalData.token
            },
            success:function(res){
              console.log(res)
              wx.setStorageSync('collect', res.data)
              that.setData({
                clickCollect:'取消收藏',
                collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c18.png'
              })
            }
          })
        }
      })
    }
    if(number2!=0){
      wx.request({
        url: 'https://wx.xxmtcds.top:2333/user/collectmusic',
        method:'POST',
        name:'collectList',
        data:{
          id:that.data.id
        },
        header:{
          'Content-Type': 'application/json',
            authorization:app.globalData.token
        },
        success:function(res){
          console.log(res)
          wx.request({
            url: 'https://wx.xxmtcds.top:2333/user/collectmusic',
            method:'GET',
            name:'collectList',
            header:{
            'Content-Type': 'application/json',
            authorization:app.globalData.token
      
          },
          success:function(res){
            console.log(res)
            that.setData({
              clickCollect:'点击收藏',
              collect:'https://pic.imgdb.cn/item/619b4e652ab3f51d91810c30.png'
            })
            wx.setStorageSync('collect', res.data)
          }
          })
        }
      })
    }

   }
})